﻿Imports Framework.Data

Friend Class Settings
    Private Shared mSettingsRead As Boolean = False
    Private Shared mIncomingFilesDirectory As String
    Private Shared mPauseDownloadingBelowVolumeBytesFree As Long
    Private Shared mRecycleBelowVolumeBytesFree As Long
    Private Shared mIncomingDownloadsGrantedToSecurityGroupIds As New HashSet(Of Integer)

    Friend Shared ReadOnly SettingsKey As String = "FileShare"

    Public Shared ReadOnly Property IncomingFilesDirectory() As String
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mIncomingFilesDirectory
        End Get
    End Property
    Public Shared Sub SetIncomingFilesDirectory(ByVal value As String, ByVal transactionScope As TransactionScope)
        If Not mSettingsRead Then LoadSettings()
        If mIncomingFilesDirectory <> value Then
            Framework.Data.MetaData.SetValue("FileShare_IncomingFilesDirectory", value, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mIncomingFilesDirectory", value)
        End If
    End Sub

    Public Shared Function IncomingDownloadsGrantedToSecurityGroupIds() As Generic.HashSet(Of Integer)
        If Not mSettingsRead Then LoadSettings()
        Return mIncomingDownloadsGrantedToSecurityGroupIds
    End Function
    Public Shared Function IsGroupIncomingDownloadsGrantedToSecurityGroupIds(ByVal securityGroupId As Integer) As Boolean
        If Not mSettingsRead Then LoadSettings()
        Return mIncomingDownloadsGrantedToSecurityGroupIds.Contains(securityGroupId)
    End Function
    Public Shared Sub SetIncomingDownloadsGrantedToSecurityGroupIds(ByVal value As Generic.HashSet(Of Integer), ByVal transactionScope As TransactionScope)
        If Not mSettingsRead Then LoadSettings()
        Framework.Data.MetaData.SetValue("FileShare_IncomingDownloadsGrantedToSecurityGroupIds", Framework.Serialization.ToDBSeparatorString(mIncomingDownloadsGrantedToSecurityGroupIds, ";"c), transactionScope)
        transactionScope.SetClassFieldValue(GetType(Settings), "mIncomingDownloadsGrantedToSecurityGroupIds", value)
    End Sub

    Public Shared ReadOnly Property DownloadingFilesDirectory() As String
        Get
            If String.IsNullOrEmpty(IncomingFilesDirectory) Then
                Return Nothing
            Else
                Return IO.Path.Combine(IncomingFilesDirectory, "Downloading")
            End If
        End Get
    End Property
    Public Shared ReadOnly Property CompleteFilesDirectory() As String
        Get
            If String.IsNullOrEmpty(IncomingFilesDirectory) Then
                Return Nothing
            Else
                Return IO.Path.Combine(IncomingFilesDirectory, "Complete")
            End If
        End Get
    End Property
    Public Shared ReadOnly Property RecycledFilesDirectory() As String
        Get
            If String.IsNullOrEmpty(IncomingFilesDirectory) Then
                Return Nothing
            Else
                Return IO.Path.Combine(IncomingFilesDirectory, "Recycled")
            End If
        End Get
    End Property

    Public Shared ReadOnly Property RecycleBelowVolumeBytesFree() As Long
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mRecycleBelowVolumeBytesFree
        End Get
    End Property
    Public Shared Sub SetRecycleBelowVolumeBytesFree(ByVal value As Long, ByVal transactionScope As TransactionScope)
        If Not mSettingsRead Then LoadSettings()
        If mRecycleBelowVolumeBytesFree <> value Then
            Framework.Data.MetaData.SetValue("FileShare_RecycleBelowVolumeBytesFree", value.ToString, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mRecycleBelowVolumeBytesFree", value)
        End If
    End Sub

    Public Shared ReadOnly Property PauseDownloadingBelowVolumeBytesFree() As Long
        Get
            If Not mSettingsRead Then LoadSettings()
            Return mPauseDownloadingBelowVolumeBytesFree
        End Get
    End Property
    Public Shared Sub SetPauseDownloadingBelowVolumeBytesFree(ByVal value As Long, ByVal transactionScope As TransactionScope)
        If Not mSettingsRead Then LoadSettings()
        If mRecycleBelowVolumeBytesFree <> value Then
            Framework.Data.MetaData.SetValue("FileShare_PauseDownloadingBelowVolumeBytesFree", value.ToString, transactionScope)
            transactionScope.SetClassFieldValue(GetType(Settings), "mPauseDownloadingBelowVolumeBytesFree", value)
        End If
    End Sub

    Private Shared Sub LoadSettings()
        mIncomingFilesDirectory = Framework.Data.MetaData.GetValueAsString("FileShare_IncomingFilesDirectory", Nothing)
        mRecycleBelowVolumeBytesFree = CLng(Framework.Data.MetaData.GetValueAsString("FileShare_RecycleBelowVolumeBytesFree", CStr(150 * 1024 * 1024)))
        mPauseDownloadingBelowVolumeBytesFree = CLng(Framework.Data.MetaData.GetValueAsString("FileShare_PauseDownloadingBelowVolumeBytesFree", CStr(100 * 1024 * 1024)))
        mIncomingDownloadsGrantedToSecurityGroupIds = Framework.Serialization.FromDBSeparatorStringIntegersHashed(Framework.Data.MetaData.GetValueAsString("FileShare_IncomingDownloadsGrantedToSecurityGroupIds", ";;"))
        If mIncomingDownloadsGrantedToSecurityGroupIds.Count = 1 And mIncomingDownloadsGrantedToSecurityGroupIds.Contains(-1) Then mIncomingDownloadsGrantedToSecurityGroupIds.Clear()
        mSettingsRead = True
    End Sub

End Class
